import { McData } from './McData';
//import begin

/**
 * 主要处理模块层Res
 */
export class ResManager {
    private static _Singleton: ResManager;
    public mcDataMap: { [key: string]: McData } = {}

    public static get Singleton(): ResManager {
        if (this._Singleton == null) {
            this._Singleton = new ResManager();
        }
        return this._Singleton;
    }

    /**
     * 使用Promise加载资源数组
     */
    public loadArrayP(urlArray: Array<any>, progressHandler: Laya.Handler = null): Promise<boolean> {
        return new Promise((resolve: any, reject: any) => {
            let completeHandler: Laya.Handler = Laya.Handler.create(null, (data: any) => {
                if (data) {
                    resolve(data);
                }
                else {
                    reject({ message: 'load urlArray error ' + urlArray });
                }
            });

            Laya.loader.load(urlArray, completeHandler, progressHandler);
        });
    }

    /**
     * 使用Promise加载文本
     */
    public loadTextP(url: string): Promise<string> {
        return new Promise((resolve: any, reject: any) => {
            let handler: Laya.Handler = Laya.Handler.create(null, (data: any) => {
                if (data != null) {
                    resolve(data);
                }
                else {
                    reject({ message: 'load text error' });
                }
            });

            Laya.loader.load(url, handler, null, Laya.Loader.TEXT);
        });
    }

    /**
     * 使用Promise加载Json
     */
    public loadJsonP(url: string, custom_priority: number = -1): Promise<any> {
        return new Promise((resolve: any, reject: any) => {
            let handler: Laya.Handler = Laya.Handler.create(null, (data: any) => {
                if (data != null) {
                    resolve(data);
                }
                else {
                    reject({ message: 'load json error' });
                }
            });
            Laya.loader.load(url, handler, null, Laya.Loader.JSON, custom_priority >= 0 ? custom_priority : 0);
        });
    }

    /**
     * 使用Promise加载图片
     */
    public loadImageP(url: string): Promise<any> {
        url = Laya.URL.formatURL(url);
        return new Promise((resolve: any, reject: any) => {
            let handler: Laya.Handler = Laya.Handler.create(this, (data: any) => {
                if (data != null) {
                    let tex: Laya.Texture = data as Laya.Texture;
                    if (tex instanceof Laya.Texture2D) {
                        console.debug("#### Texture2D reload to texture");
                        Laya.loader.load(url, handler, null, Laya.Loader.IMAGE, 1, true, null, false, true);
                        return;
                    }
                    tex.url = url;

                    if (tex && tex.bitmap) {
                        // tex.bitmap.enableMerageInAtlas = false;
                    }

                    resolve(tex);
                }
                else {
                    reject({ message: 'load image error  ' + url });
                }
            });

            // Laya.loader.load(url, handler, null, Laya.Loader.IMAGE);
            Laya.loader.load(url, handler, null, Laya.Loader.IMAGE, 0, true, null, false, true);
        });
    }

    public setMcData(mcData: McData): void {
        this.mcDataMap[mcData.getUrl()] = mcData;
    }

    public getMcData(url: string): McData {
        return this.mcDataMap[url];
    }

    public clearMcData(): void {
        for (let key in this.mcDataMap) {
            delete this.mcDataMap[key];
        }
    }




}


